/*
** 创建时间：200501
** 文件名：windows.hpp
** 用途：多窗口
*/

#include "xege.h"
#include <thread>
#include <functional>
#include <string>

// #include "image.hpp"
namespace xege {

	WINDOW::WINDOW (int w, int h, int x, int y, int winMode, int renMode) {
		// SDL_Log("window is openning");
		// printf ("%d %d %d %d %d %d\n", w, h, x, y, winMode, renMode);
		this -> winEvent = new std::thread ([&](int w, int h, int x, int y, int winMode, int renMode) {
			title = "XEGE-SDL 20.07 S W: "/* + std::to_string (id)*/;
			if (!SDL_WasInit (SDL_INIT_EVERYTHING) && SDL_Init (SDL_INIT_EVERYTHING) != 0) {
				SDL_Log ("SDL_Init error: %s", SDL_GetError ());
				IMG_Init (IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF | IMG_INIT_WEBP);
				Mix_Init (MIX_INIT_FLAC | MIX_INIT_MID | MIX_INIT_MOD | MIX_INIT_MP3 | MIX_INIT_OGG);
			}
			FGColor = 0xffffffff;
			BGColor = 0x000000ff;
			// printf ("%d %d %d %d %d %d\n", w, h, x, y, winMode, renMode);
			window = SDL_CreateWindow (title.c_str (), x, y, w, h, winMode);

			// 创建窗口
			if (window == nullptr) {
				// 输出错误信息
				std::cerr << "Create Window Fail: " << SDL_GetError ();
				// 退出
				SDL_Quit ();
			} else {
				// 获取 window 的 render
				// std::cerr << "something Fail: " << SDL_GetError ();
				rPic = SDL_CreateRenderer (window, -1, renMode);
				// rPic = SDL_CreateRenderer (window, -1, SDL_RENDERER_ACCELERATED);
				// rPic = SDL_CreateRenderer (window, -1, SDL_RENDERER_PRESENTVSYNC);
				// rPic = SDL_CreateRenderer (window, -1, SDL_RENDERER_TARGETTEXTURE);
				// id ++;
				if (rPic == nullptr) {
					std::cerr << "Create Renderer Fail: " << SDL_GetError ();
					SDL_Quit ();
				} else {
					// 获取 window 的 surface
					sPic = SDL_GetWindowSurface (window);
					if (sPic == nullptr) {
						std::cerr << "Create Surface Fail: " << SDL_GetError ();
						SDL_Quit ();
					}
					// 从 rPic 创建一个 texture 纹理
					tPic = SDL_CreateTexture (rPic, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, w, h);
					// 啥啥啥啥啥？？？？？这个像素颜色格式好像不对头啊，我存的不一直都是RGBA么？为什么RGBA反而输出有问题了
					// SDL_SetTextureColorMod ()
					if (tPic == nullptr) {
						std::cerr << "Create Texture Fail: " << SDL_GetError ();
						SDL_Quit ();
					} else {
						// 设置背景色
						SDL_SetRenderDrawColor (rPic, XEGEGET_R (BGColor), XEGEGET_G (BGColor), XEGEGET_B (BGColor), XEGEGET_A (BGColor));
						SDL_RenderDrawRect (rPic, nullptr);
						SDL_RenderPresent (rPic);
						SDL_UpdateWindowSurface (window);
					}
				}


			}
			this -> isRunFlag = true;
			for (; isRun () ; SDL_Delay(2)) {
				switch (SDL_PollEvent (event); event -> type) {
					case SDL_QUIT: {
						this -> isRunFlag = false;
						close ();
						SDL_Quit ();
					} break;
					case SDL_APP_TERMINATING: {
					} break;
					case SDL_APP_LOWMEMORY: {
					} break;
					case SDL_APP_WILLENTERBACKGROUND: {
					} break;
					case SDL_APP_DIDENTERBACKGROUND: {
					} break;
					case SDL_APP_WILLENTERFOREGROUND: {
					} break;
					case SDL_APP_DIDENTERFOREGROUND: {
					} break;
					case SDL_DISPLAYEVENT: {
					} break;
					case SDL_WINDOWEVENT: {
					} break;
					case SDL_SYSWMEVENT: {
					} break;
					case SDL_KEYDOWN: {
						events["keydown"](SDL_KEYDOWN);
					} break;
					case SDL_KEYUP: {
					} break;
					case SDL_TEXTEDITING: {
					} break;
					case SDL_TEXTINPUT: {
					} break;
					case SDL_KEYMAPCHANGED: {
					} break;
					case SDL_MOUSEMOTION: {
					} break;
					case SDL_MOUSEBUTTONDOWN: {
					} break;
					case SDL_MOUSEBUTTONUP: {
					} break;
					case SDL_MOUSEWHEEL: {
					} break;
					case SDL_JOYAXISMOTION: {
					} break;
					case SDL_JOYBALLMOTION: {
					} break;
					case SDL_JOYHATMOTION: {
					} break;
					case SDL_JOYBUTTONDOWN: {
					} break;
					case SDL_JOYBUTTONUP: {
					} break;
					case SDL_JOYDEVICEADDED: {
					} break;
					case SDL_JOYDEVICEREMOVED: {
					} break;
					case SDL_CONTROLLERAXISMOTION: {
					} break;
					case SDL_CONTROLLERBUTTONDOWN: {
					} break;
					case SDL_CONTROLLERBUTTONUP: {
					} break;
					case SDL_CONTROLLERDEVICEADDED: {
					} break;
					case SDL_CONTROLLERDEVICEREMOVED: {
					} break;
					case SDL_CONTROLLERDEVICEREMAPPED: {
					} break;
					case SDL_FINGERDOWN: {
					} break;
					case SDL_FINGERUP: {
					} break;
					case SDL_FINGERMOTION: {
					} break;
					case SDL_DOLLARGESTURE: {
					} break;
					case SDL_DOLLARRECORD: {
					} break;
					case SDL_MULTIGESTURE: {
					} break;
					case SDL_CLIPBOARDUPDATE: {
					} break;
					case SDL_DROPFILE: {
					} break;
					case SDL_DROPTEXT: {
					} break;
					case SDL_DROPBEGIN: {
					} break;
					case SDL_DROPCOMPLETE: {
					} break;
					case SDL_AUDIODEVICEADDED: {
					} break;
					case SDL_AUDIODEVICEREMOVED: {
					} break;
					case SDL_SENSORUPDATE: {
					} break;
					case SDL_RENDER_TARGETS_RESET: {
					} break;
					case SDL_RENDER_DEVICE_RESET: {
					} break;
					case SDL_USEREVENT: {
					} break;
					default : {
					} break;
				}
			}
		}, w, h, x, y, winMode, renMode);
		// this->event.swap(window_thread);
		// SDL_Log("window is opened");
		this -> winEvent -> detach();
		// std::function <int(Uint32)> eventSolvers;
	}
	WINDOW& WINDOW::operator=(const WINDOW& window) {
		this -> window = window.window;
		this -> title = window. title;
	}
	
	WINDOW::~WINDOW (void) {
		// SDL_Log("window is closed");
		close ();
	}
	WINDOW::operator bool() {
		return this -> isRunFlag;
	}



	// friend class IMAGE;
	WINDOW& WINDOW::setTitle (std::string title) {
		this -> title = title;
		SDL_SetWindowTitle (window, title.c_str ());
		return *this;
	}
	// WINDOW (int w, int h, int winMode = WINDOW_RESIZABLE, int renMode = SDL_RENDERER_SOFTWARE) {
	// 	createWindow (w, h, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, winMode, renMode);
	// }
	WINDOW& WINDOW::clear () {
		SDL_SetRenderDrawColor (rPic, BG.r, BG.g, BG.b, BG.a);
		SDL_RenderClear (rPic);
		SDL_FillRect (sPic, nullptr, BGColor);
		return *this;
		// SDL_MapRGBA (SDL_PIXELFORMAT_BGRA32, 0, 0, 0, 255);
	}
	WINDOW& WINDOW::updateTexture (void) {
		SDL_UpdateTexture(tPic, nullptr, sPic -> pixels, sPic -> pitch);
		SDL_RenderCopy(rPic, tPic, nullptr, nullptr);
		return *this;
	}
	WINDOW& WINDOW::update (void) {
		// SDL_RenderPresent(g_ptrMainWnd->m_sdlrenderer_pgm);
		// SDL_UpdateWindowSurface (window);
		SDL_RenderPresent (rPic);
		// SDL_UpdateTexture (tPic);
		return *this;
	}
	
	WINDOW& WINDOW::close () {
		isRunFlag = false;
		SDL_DestroyWindow (window);
		return *this;
	}
	WINDOW& WINDOW::show (void) {
		SDL_ShowWindow (window);
		return *this;
	}
	WINDOW& WINDOW::hide (void) {
		SDL_HideWindow (window);
		return *this;
	}
	// 置顶
	WINDOW& WINDOW::raise (void) {
		SDL_RaiseWindow (window);
		return *this;
	}

	bool WINDOW::isRun (void) {
		return this -> isRunFlag;
	}

	SDL_Window* WINDOW::getpWindow (void) {
		return window;
	}
	SDL_Window& WINDOW::getpWindow (void) {
		return *this;
	}

}  // 命名空间 xege 结尾